package com.yh.workbill.ui.addworkbill.mvp;

import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.base.IBaseView;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.model.WorkDevices;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class AddWorkBill_Model {

   public interface AddCallBack extends IBaseModel.Fail {
        void Success(String msg, String company, String unitCompany, WorkBill workBill);
    }

    public interface DevicesBack extends IBaseModel.Fail {
        void Suceess(List<WorkDevices> data);
    }

    public void addWorkBill(WorkBill workBill, AddCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            workBill.setUser_uuid(singleUserInfo.getUuid());
            OkHttpService.getInstance().addWorkBill(workBill.getUser_uuid(),
                    workBill.getWork_time(), workBill.getWork_unit(), workBill.getWork_content(), workBill.getWork_car_number(),
                    workBill.getWork_device(), workBill.getWork_use_time(), workBill.getCompany_name(), workBill.getWork_time_price(), new BaseCallback<String>() {
                        @Override
                        public void accessSuccess(int code, String msg, String data) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                                if (code == 200) {
                                    callBack.Success(data, workBill.getCompany_name(), workBill.getWork_unit(), workBill);
                                } else {
                                    callBack.fail(data);
                                }
                            });
                        }

                        @Override
                        public void accessFailure(String errorMessage) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                        }
                    });
        });

    }

    public void getAllDevices(DevicesBack callBack) {
        OkHttpService.getInstance().getDevices(new BaseCallback<List<WorkDevices>>() {

            @Override
            public void accessSuccess(int code, String msg, List<WorkDevices> data) {
                if (code == 200) {
                    callBack.Suceess(data);
                } else {
                    callBack.fail(msg);
                }
            }

            @Override
            public void accessFailure(String errorMessage) {
                callBack.fail(errorMessage);
            }
        });
    }

}
